﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PlayerAttackAndDamage : AttackAndDamage {
    // 继承后 拓展项目写在里面


    public float attackB = 100;

    public float attackRange = 90;

    public float attackGun = 200;

    public GunUseController gun;// 持续实例化子弹的脚本

    public AudioClip swordSingSource;
    public AudioClip gunShotSource;


	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    // 处理A事件伤害
    public void AttackAEvent()
    {
        AudioSource.PlayClipAtPoint(swordSingSource, transform.position, 1.0f);

        GameObject enemy = null;
        float distance = base.attackDistance;
        // 找怪物
        foreach (GameObject Obj in EnemyManager._instance.enemyListArray)
        {
            float tempDistance = Vector3.Distance(Obj.transform.position,transform.position);

            if (tempDistance <= distance)
            {
                enemy = Obj;
                // 取最近的怪物
                distance = Vector3.Distance(Obj.transform.position , transform.position);
            }
        }

        // 判断在攻击距离以内是否有敌人
        if (enemy)
        {
            Vector3 targetPos = enemy.transform.position;
            targetPos.y = transform.position.y;
            transform.LookAt(targetPos);
            if(GetComponent<GetAwardSphere>().weaponName.Equals("single"))
            enemy.GetComponent<AttackAndDamage>().TakenDamage(base.normalAttack);
            else if(GetComponent<GetAwardSphere>().weaponName.Equals("dual"))
                enemy.GetComponent<AttackAndDamage>().TakenDamage(base.normalAttack+40);
        }
    }

    // 处理B事件伤害
    public void AttackBEvent()
    {
        AudioSource.PlayClipAtPoint(swordSingSource, transform.position, 1.0f);
        GameObject enemy = null;
        float distance = base.attackDistance;

        foreach (GameObject Obj in EnemyManager._instance.enemyListArray)
        {
            float tempPos = Vector3.Distance(Obj.transform.position,transform.position);
            if (tempPos <= distance)
            {
                enemy = Obj;

                distance = Vector3.Distance(Obj.transform.position, transform.position);

            }
        }

        if (enemy)
        {
            Vector3 target = enemy.transform.position;
            target.y = transform.position.y;
            transform.LookAt(target);
            if (GetComponent<GetAwardSphere>().weaponName.Equals("single"))
                enemy.GetComponent<AttackAndDamage>().TakenDamage(attackB);
            else if (GetComponent<GetAwardSphere>().weaponName.Equals("dual"))
                enemy.GetComponent<AttackAndDamage>().TakenDamage(attackB + 40);
        }

    }
    // 处理Range事件伤害
    public void AttackRangeEvent()
    {
        AudioSource.PlayClipAtPoint(swordSingSource, transform.position, 1.0f);
        List<GameObject> enemyList = new List<GameObject>();
        // 将范围内的敌人加入集合之内 然后进行伤害
        foreach (GameObject Obj in EnemyManager._instance.enemyListArray)
        {
            float tempDistance = Vector3.Distance(Obj.transform.position, transform.position);

            if (tempDistance<attackDistance)
            {
                enemyList.Add(Obj);
            }
        }
        

        if (enemyList.Count>0)
        {
            foreach (GameObject Obj in enemyList)
            {
                if (GetComponent<GetAwardSphere>().weaponName.Equals("single"))
                    Obj.GetComponent<AttackAndDamage>().TakenDamage(attackRange);
                else if (GetComponent<GetAwardSphere>().weaponName.Equals("dual"))
                    Obj.GetComponent<AttackAndDamage>().TakenDamage(attackRange+40);
            }
            
        }
    }
    // 处理Gun事件伤害
    public void AttackGunEvent()
    {
        AudioSource.PlayClipAtPoint(gunShotSource, transform.position, 1.0f);
        gun.Shot();
    }

}
